
var util = require('../../utils/util.js')
var app = getApp();
var key = app.globalData.key;
const resurl = app.globalData.resurl;
let interstitialAd = null;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isDelay: '',
    adverShow: true,
    seconds: 15,
    time: 15,
    money: '',
    timeShow: false,
    showMaskNext: false,
    timeyuanbao: '',
    userMoney: '',
  },
  timerPost: '',
  insertScreenAdTimer:'',



  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var isDelay = wx.getStorageSync("isDelay");
    that.setData({
      isDelay,
    })
  },
  // 关闭banner广告
  adClose: function () {
    this.setData({
      adverShow: false,
      // videoShow: true,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    that.setData({
      money: app.globalData.money,
    })
    var seconds = 15;
    var time = 15;
    var money = that.data.money;
    var timer = setInterval(function () {
      that.setData({
        seconds: --seconds
      })
      if (seconds === 0) {
        that.setData({
          seconds: 15,
          time: 0
        })
        clearInterval(timer)
      }
    }, 1000)
    var timerPost = setInterval(function () {
      //眼保健操 接口13
      var userId = wx.getStorageSync("userId");
      var data13 = {
        deviceProperties: app.globalData.deviceProperties,
        user: {
          userId: userId,
          jobId: 9,
        }
      }
      data13 = JSON.stringify(data13);
      data13 = util.base64_encode(data13);
      data13 = util.xxtea_encrypt(data13, key);
      wx.request({
        url: resurl + '/interface/sdkData?requestId=13&a=1&b=2',
        data: data13,
        header: {
          "content-type": "application/json",
          "dataType": "json",
        },
        method: "post",
        success: function (res) {
          res = util.xxtea_decrypt(res.data, key);
          res = util.base64_decode(res);
          res = JSON.parse(res);
          console.log(res);
          if (res.resultCode === 0) {
            res = JSON.parse(res.data);
            console.log(res);
            // that.timeToast(1000);测试
            if (res.getMoney) {
              app.globalData.money = money + res.getMoney;
              that.setData({
                money: app.globalData.money,
              })
              that.timeToast(res.getMoney);
            }
          }
        }
      })
      clearInterval(timerPost);
    }, 15000)
    that.timerPost = timerPost;
    var count = 0;
    var insertScreenAdTimer = setInterval(function () {
      count ++;
      console.log(count);
      if(count > 2) {
        that.insertScreenAD();
        clearInterval(insertScreenAdTimer);
      }
    },1000)
    that.insertScreenAdTimer = insertScreenAdTimer;
  },
  insertScreenAD: function () {
    // 在页面onLoad回调事件中创建插屏广告实例
    if (wx.createInterstitialAd) {
      interstitialAd = wx.createInterstitialAd({
        adUnitId: 'adunit-1cc4cb154d00aa08'
      })
      interstitialAd.onLoad(() => { })
      interstitialAd.onError((err) => { })
      interstitialAd.onClose(() => { })
    }

    // 在适合的场景显示插屏广告
    if (interstitialAd) {
      interstitialAd.show().catch((err) => {
        console.error(err)
      })
    }
  },
  // 显示领取元宝奖励的弹窗
  timeToast: function (a) {
    var that = this;
    var money = that.data.money;
    that.setData({
      timeShow: true,
      showMaskNext: true,
      timeyuanbao: a,
    })
    var userMoney = Math.floor(money / 10000 * 100) / 100;
    that.setData({
      userMoney: userMoney,
    })
  },
  // 关闭领取奖励的弹窗
  clickcloseReward: function () {
    var that = this;
    var isDelay = that.data.isDelay;
    if(isDelay == 1) {
      var delayTimer = setInterval(function () {
        that.setData({
          timeShow: false,
          showMaskNext: false,
        })
        clearInterval(delayTimer);
      },2000)
    } else {
      that.setData({
        timeShow: false,
        showMaskNext: false,
      })
    }
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    var that = this;
    clearInterval(that.timerPost);
    clearInterval(that.insertScreenAdTimer);
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    var that = this;
    clearInterval(that.timerPost);
    clearInterval(that.insertScreenAdTimer);
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})